<template>
  <div class="updateBirthday">
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('colose-birthday')"
      @confirm="onSubmit"
    />
  </div>
</template>
<script>
import { setUserInfo } from '@/api/user'
import { Toast } from 'vant'
import dayjs from 'dayjs'
export default {
  name: 'UpdateBirthday',
  data () {
    return {
      minDate: new Date(1949, 10, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },
  created () {},
  methods: {
    async onSubmit (value) {
      const birthday = dayjs(value).format('YYYY-MM-DD')
      Toast.loading({
        message: '保存中...',
        forbidClick: true
      })
      try {
        await setUserInfo({
          birthday
        })
        this.$emit('colose-birthday')
        Toast.success('保存成功')
        this.$emit('input', birthday)
      } catch (error) {
        Toast.fail('修改失败')
        return error
      }
    }
  },
  components: {},
  props: {
    value: {
      type: [Number, String],
      required: true
    }
  },
  watch: {},
  computed: {},
  mounted () {}
}
</script>

<style lang="less" scoped></style>
